package pdm.yamba.feed;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.util.Log;

import pdm.yamba.dto.Status;
import pdm.yamba.listener.IStatusPublishListener;
import pdm.yamba.listener.IStatusRefreshListener;
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.Twitter.User;
import winterwell.jtwitter.TwitterException;

public class TwitterFeed
{
	Twitter twitter = null;
	List<IStatusRefreshListener> sListeners = new ArrayList<IStatusRefreshListener>();
	List<IStatusPublishListener> spListeners = new ArrayList<IStatusPublishListener>();
	
	public TwitterFeed() 
	{
		
	}

	public TwitterFeed(String username, String password, String serviceUri) 
	{
		Login(username, password);
		twitter.setAPIRootUrl(serviceUri);
	}
	
	public boolean Login(String username, String password) 
	{
		try 
		{
			twitter = new Twitter(username, password);
			return true;
		}
		catch(TwitterException e)
		{
			Log.e("TwitterService", e.getMessage());
			return false;
		}
	}
	
	public User GetUserInfo(String username)
	{
		return twitter.getUser(username);
		
	}
	
	public void PublishViaService(String message) 
	{
		twitter.updateStatus(message);
		
	}	
	
	public void Publish(Status status) 
	{
		new PublishStatusTask().execute(status);
	}
	
	public List<pdm.yamba.dto.Status> RefreshViaService(Integer statusesLength)
	{
		List<pdm.yamba.dto.Status> statuses = new ArrayList<pdm.yamba.dto.Status>();
    	List<Twitter.Status> twitterStatuses = twitter.getHomeTimeline();
    	pdm.yamba.dto.Status currentStatus;
    	for(Twitter.Status tStatus : twitterStatuses) 
    	{
	    	currentStatus = new pdm.yamba.dto.Status(tStatus);
	    	statuses.add(currentStatus);
    	}
    	return statuses;
	}	
	
	public void Refresh(Integer statusesLength)
	{
		new RefreshStatusTask().execute(statusesLength);
	}
	
	public void addStatusRefreshListener(IStatusRefreshListener eventListener) 
	{
		sListeners.add(eventListener);
	}
	
	public void addStatusPublishListener(IStatusPublishListener eventListener) 
	{
		spListeners.add(eventListener);
	}
	
	private class RefreshStatusTask extends AsyncTask<Integer, Status, List<pdm.yamba.dto.Status>> 
	{  	
		@Override		
		protected List<pdm.yamba.dto.Status> doInBackground(Integer... statusesLength) 
		{
			List<pdm.yamba.dto.Status> statuses = new ArrayList<pdm.yamba.dto.Status>();
            try 
            {	
            	List<Twitter.Status> twitterStatuses = twitter.getHomeTimeline();
            	pdm.yamba.dto.Status currentStatus;
    	    	for(Twitter.Status tStatus : twitterStatuses) 
    	    	{
    	    		//if(statuses.size() < statusesLength[0]) 
    	    		//{
	    	    		currentStatus = new pdm.yamba.dto.Status(tStatus);
	    	    		statuses.add(currentStatus);
	    	    		publishProgress(currentStatus);
    	    		//}
    	    		//else 
    	    		//{
    	    		//	this.cancel(true);
    	    		//	break;
    	    		//}
    	    		
    	    		if (isCancelled()) break;
    	    	}
    	    	if (isCancelled()) 
    	    		return statuses;
            }
    		catch(TwitterException e) 
    		{
    			Log.e("TwitterStorage", e.getMessage());
    		}
            
			return statuses;
        }

		@Override
		protected void onProgressUpdate(pdm.yamba.dto.Status... status) 
		{
	    	for (IStatusRefreshListener hl : sListeners) 
	    		hl.onRefreshProgress(status[0]);
		}

		@Override       
		protected void onPostExecute(List<pdm.yamba.dto.Status> statuses) 
		{
	    	for (IStatusRefreshListener hl : sListeners) 
	    		hl.onRefreshComplete(statuses);
        }
	}
	
	private class PublishStatusTask extends AsyncTask<Status, Status, Status> 
	{
		 @Override
	    protected void onPreExecute() 
		{
	    	for (IStatusPublishListener hl : spListeners) 
	    		hl.onPublishStart();
		}
		
		@Override
		protected pdm.yamba.dto.Status doInBackground(pdm.yamba.dto.Status... statuses) 
		{	
			/*
			try
			{
				Thread.sleep(5000);
			}
			catch (InterruptedException ex)
			{}
			*/
			twitter.updateStatus(statuses[0].getMessage());
			return statuses[0];			
		}
		
		@Override
		protected void onProgressUpdate(pdm.yamba.dto.Status... status) 
		{
	    	for (IStatusPublishListener hl : spListeners) 
	    		hl.onPublishProgress(status[0]);
		}
		
		@Override 
		protected void onPostExecute(pdm.yamba.dto.Status status) 
		{	    	
			for (IStatusPublishListener hl : spListeners) 
				hl.onPublishComplete(status);

		}
	}
}
